﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DTO
{
    public class BLWorktimeSheet
    {
        private Dictionary<Guid, AgentWorktime> worktimes;
        private int month;
        private int year;

        public Dictionary<Guid, AgentWorktime> Worktimes
        {
            get { return worktimes; }
        }

        public int Year
        {
            get { return year; }
        }

        public int Month
        {
            get { return month; }
        }

        public BLWorktimeSheet(int year, int month)
        {
            this.year = year;
            this.month = month;
            worktimes = new Dictionary<Guid, AgentWorktime>();
        }

        public AgentWorktime AddAgentWorktime(Guid agentId, string fullName)
        {
            AgentWorktime agentWorktime = new AgentWorktime(agentId, fullName, year, month);
            worktimes.Add(agentId, agentWorktime);
            return agentWorktime;
        }

        public void SetItem(Guid agentId, string fullName, int day, int duration, bool isApproved)
        {
            AgentWorktime agentWorktime = null;
            try
            {
                agentWorktime = worktimes[agentId];
            }
            catch (KeyNotFoundException knfExp)
            {
                agentWorktime = AddAgentWorktime(agentId, fullName);
                Console.Out.WriteLine(knfExp.Message);
            }
            if ((day > 0) && (day <= agentWorktime.Days.Count))
            {
                agentWorktime.Days[day-1].Duration = duration;
                agentWorktime.Days[day - 1].Approved = isApproved;
            }
        }
    }

    public class AgentWorktime
    {
        private Guid agentId;
        private string fullName;
        private List<DateItem> days;

        public Guid AgentId
        {
            get { return agentId; }
        }

        public List<DateItem> Days
        {
            get { return days; }
        }

        public string FullName
        {
            get { return fullName; }
        }

        public AgentWorktime(Guid agentId, string fullName, int year, int month)
        {
            this.agentId = agentId;
            this.fullName = fullName;
            days = new List<DateItem>();
            int daysInMonth = DateTime.DaysInMonth(year, month);
            for (int i = 1; i <= daysInMonth; i++)
            {
                DateTime startDate = new DateTime(year, month, i);
                days.Add(new DateItem(startDate, 0));
            }
        }
    }

    public class DateItem
    {
        private DateTime startDate;
        private int duration;
        private bool approved;

        public bool Approved
        {
            get { return approved; }
            set { approved = value; }
        }

        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }

        public int Duration
        {
            get { return duration; }
            set { duration = value; }
        }

        public DateItem(DateTime startDate, int duration)
        {
            this.startDate = startDate;
            this.duration = duration;
        }

        public double DurationInDay
        {
            get
            {
                return Convert.ToDouble(duration) / Convert.ToDouble(Constant.WorkingMinutesPerDay);
            }
        }
    }
}
